<?php 
//  QQ登录接口类，如有BUG请联系本人！！ 
/*===========================================================
*   name : QQ
*   author : `Jn.Too'
*   QQ : 274987096
*   E-mail : 274987096@qq.com
*   VERSION : 1.1v
*   DATE : 2012-1-7
*   尊重作者,保留版权信息
*   版权所有 `Jn.Too'
*   使用；不允许对程序代码以任何形式任何目的的再发布。
**/


if (defined('WEBSITE'))
{
	global $_LANG;
	$_LANG['help']['APP_KEY'] = '在 QQ http://connect.qq.com/ 此处申请的 APP ID';
	$_LANG['help']['APP_SECRET'] = '在QQ中申请的 KEY';
	
	$_LANG['APP_KEY'] = 'APP ID';
	$_LANG['APP_SECRET'] = 'KEY';
	
	
	$i = isset($web) ? count($web) : 0;
	// 类名
	$web[$i]['name'] = 'QQ';
	
	// 文件名，不包含后缀
	
	$web[$i]['type'] = 'qq';
	
	$web[$i]['className'] = 'qq';
	
	// 作者信息
	$web[$i]['author'] = '`Jn.Too\'';
	
	// 作者QQ
	$web[$i]['qq'] = '274987096';
	
	// 作者邮箱
	$web[$i]['email'] = '274987096@qq.com';
	
	// 申请网址
	$web[$i]['website'] = 'http://open.qq.com';
	
	// 版本号
	$web[$i]['version'] = '1.1v';
	
	// 更新日期
	$web[$i]['date']  = '2012-2-23';
	
	// 配置信息
	$web[$i]['config'] = array(
		array('type'=>'text' , 'name'=>'APP_KEY', 'value'=>''),
		array('type'=>'text' , 'name' => 'APP_SECRET' , 'value' => ''),
	);
}

if (!defined('WEBSITE'))
{
	class website extends cls_http
	{
		public $app_id;   //申请的 APP ID
		public $key;  // 申请的 KEY
		public $scope = 'get_user_info';  // 微博开放的所有接口
		public $token = array();
		
		function __construct()
		{
			$this->website();
		}
		
		function website()
		{
			$this->app_id = APP_KEY;
			$this->key = APP_SECRET;
		}
		
		function login($callback)
		{
			return $this->GetOAuthUrl($callback);
		}
		
		function getAccessToken()
		{
			$token_url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&"
				. "client_id=" . $this->app_id. "&redirect_uri=" . urlencode($_REQUEST['callblock'])
				. "&client_secret=" . $this->key . "&code=" . $_REQUEST["code"];
			$response = $this->http($token_url , 'GET');
			
			//var_dump($response);exit;
			if(!$this->is_error($response , false))
			{
				$this->add_error('011' , '获取AccessToken失败');
				return false;
			}
			
			if(!$response)
			{
				$this->add_error('012' , '获取AccessToken失败');
				return false;
			}
			
			$params = array();
			parse_str($response, $params);
			$this->token['token'] = $params["access_token"];
			$graph_url = "https://graph.qq.com/oauth2.0/me?access_token=" . $this->token['token'];
			$user = $this->get($graph_url);
			if(!$user)
			{
				$this->echo_Error("获取User_id");
				return false;
			}
			$this->token['token_secret'] = $user['openid'];
			return $this->token;
		}
		function setAccessToken($token)
		{
			$this->token = $token;
		}
		
		function get_user_info()
		{
			$get_user_info = "https://graph.qq.com/user/get_user_info?"
				. "access_token=" . $this->token['token']
				. "&oauth_consumer_key=" . $this->app_id
				. "&openid=" . $this->token['token_secret']
				. "&format=json";
			
			$info = $this->get($get_user_info);
			if(!$info)
			{
				return false;
			}
			
			$info['name'] = $info['nickname'];
			$info['sex'] = $info['gender'] == '男' ? '1' : '0';
			$info['user_id'] = $this->token['token_secret'];
			$info['img'] = $info['figureurl_2'];
			$info['rank_id'] = RANK_ID;
			return $info;
		}
		
		function get($url)
		{
			$response = $this->http($url , 'GET');
			return $this->is_error($response);
		}
		
		function GetOAuthUrl($callback)
		{
			$state = md5(uniqid(rand(), TRUE)); //CSRF protection
			$login_url = 'https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id='. 
						 $this->app_id . '&redirect_uri=' . urlencode($callback) .
						 '&scope='.$this->scope;
			return $login_url;
		}
		
		function is_error($response , $is_chuli = true)
		{
			if (strpos($response, "callback") !== false)
			{
				$lpos = strpos($response, "(");
				$rpos = strrpos($response, ")");
				$response  = substr($response, $lpos + 1, $rpos - $lpos -1);
			}
			
			if($is_chuli)
			{
				$ret = json_decode($response , true);
				if($ret['ret'] || $ret['error'])
				{
					$this->add_error((isset($ret['ret']) ? $ret['ret'] : $ret['error']) , isset($ret['error_description']) ? $ret['error_description'] : '' , isset($ret['msg']) ? $ret['msg'] : '');
					return false;
				}
				return $ret ? $ret : $response;
			}
			else
			{
				return true;
			}
		}
		
	}
}
?>